//
//  XDLMDiscoveryViewController.m
//  xindailianmeng
//
//  Created by 许佳奇 on 17/1/17.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDLMDiscoveryViewController.h"
#import "ZHRTRequestFailureShowViewOrDetail.h"
@interface XDLMDiscoveryViewController ()<UIWebViewDelegate>

@property (nonatomic, weak) UIWebView *web;

@property (nonatomic, strong) NSURL *url;


@property (nonatomic, weak) UIView *progressHUDView;

@property (nonatomic, strong) UIView * errorView;
@end

@implementation XDLMDiscoveryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 64 - 49)];
    web.scrollView.bounces = NO;
    web.backgroundColor = [UIColor whiteColor];
    web.delegate = self;
    web.scrollView.showsVerticalScrollIndicator = NO;
    web.scrollView.showsHorizontalScrollIndicator = NO;
    self.web = web;
    [self.view addSubview:web];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSURL *url = [NSURL URLWithString:KWebUrl(@"appfind.html")];
    [self.web loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]];

}

- (void)loadData {

    [self.web loadRequest:[NSURLRequest requestWithURL:self.url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]];
}



- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    [ZHRTRequestFailureShowViewOrDetail showProgressHUDWithInView:self.progressHUDView type:0];
    self.url = request.URL;
    if (![request.URL.absoluteString isEqualToString:KWebUrl(@"appfind.html")]) {
        self.view.frame = CGRectMake(0, 64, kScreenW, kScreenH);
        webView.frame = CGRectMake(0, 0, kScreenW, kScreenH - 64);
    } else {
        self.view.frame = CGRectMake(0, 64, kScreenW, kScreenH);
        webView.frame = CGRectMake(0, 0, kScreenW, kScreenH - 64 - 49);
    }
    if (![request.URL.absoluteString isEqualToString:KWebUrl(@"appfind.html")]) {
       self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
        self.tabBarController.tabBar.hidden = YES;
    }else {
        
        self.tabBarController.tabBar.hidden = NO;
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
    }
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
     [self.progressHUDView removeFromSuperview];
    
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none'"];
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none'"];
    self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

     [self.progressHUDView removeFromSuperview];
    
    if (!_errorView) {
        
        [self.view addSubview:self.errorView ];
        
        [ZHRTRequestFailureShowViewOrDetail showRequestFailureForView:_errorView target:self action:@selector(loadData) type:0];
    }

    
}


-(void)back {
    
    // 判断两种情况: push 和 present
    if ([self.web canGoBack]) {
        [self.web goBack];
    } else if ((self.presentedViewController || self.presentingViewController) && self.childViewControllers.count == 1) {
        
        
        [self dismissViewControllerAnimated:YES completion:nil];
    } else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

- (UIView *)progressHUDView {
    if (_progressHUDView == nil) {
        UIView *view = [[UIView alloc] initWithFrame:kScreenB];
        _progressHUDView = view;
        [self.view addSubview:view];
    }
    return _progressHUDView;
}

- (UIView *)errorView {
    if (_errorView == nil) {
        _errorView = [[UIView alloc] initWithFrame:kScreenB];
    }
    return _errorView;
}

@end
